import Vue from "vue"
import Router from "vue-router"
import token from "@/utils/token"
import BasicLayout from "@/layout"
const RouteView = {
  name: "RouteView",
  render: (h) => h("router-view"),
}

Vue.use(Router)

const router = new Router({
  routes: [{
    path: "/404",
    meta: {
      title: "404",
    },
    component: () =>
      import("@/viewsLogin/errorinfo"),
  },

  //登录页面
  {
    path: "/login",
    component: () =>
      import("@/viewsLogin/index"),
    meta: {
      title: "登录",
    },
  },
  // 手机跳转中....
  {
    path: "/token",
    meta: {
      title: "页面跳转中....",
    },
    component: () =>
      import("@/viewsLogin/token"),
  },
  /* 测试页面 */
  {
    path: "/test",
    meta: {
      title: "测试页面",
    },
    component: () =>
      import("@/views/test"),
  },
  {
    path: "/home",
    meta: {
      title: "系统后台",
      auth: true,
    },
    component: BasicLayout,
    redirect: "/index", // 设置默认打开的页面
    children: [{
      path: "/index",
      name: "home",
      meta: {
        title: "首页",
        auth: true,
        keepAlive: true,
        icon: "home",
      },
      component: () =>
        import("@/views/home"),
    },
    {
      path: "/user",
      name: "user",
      meta: {
        title: "用户管理",
        auth: true,
        icon: "user-list",
      },
      component: RouteView,
      children: [{
        path: "/user/list",
        name: "user",
        meta: {
          title: "用户管理",
          auth: true,
          keepAlive: true,
          icon: "user",
        },
        component: () =>
          import("@/views/user"),
      },]
    },
    // {
    //   path: "/salemarket",
    //   name: "salemarket",
    //   meta: {
    //     title: "集市管理",
    //     auth: true,
    //     keepAlive: true,
    //     icon: "user",
    //   },
    //   component: () => import("@/views/salemarket"),
    // },

    /* cp管理 */
    // {
    //     path: '/cp',
    //     name: 'cpmanage',
    //     meta: {
    //         title: "CP管理",
    //         auth: true,
    //         icon: "share-alt"
    //     },
    //     component: RouteView,
    //     children: [
    //         {
    //             path: "/cp/incp",
    //             name: "incp",
    //             meta: {
    //                 title: "场内CP",
    //                 auth: true,
    //                 icon: "copyright"
    //             },
    //             component: () =>
    //                 import ("@/views/cp/incp/index"),
    //         },
    //         {
    //             path: "/cp/outcp",
    //             name: "outcp",
    //             meta: {
    //                 title: "场外CP",
    //                 auth: true,
    //                 icon: "sync"
    //             },
    //             component: () =>
    //                 import ("@/views/cp/outcp/index"),
    //         }
    //     ]
    // },
    // 订单系统
    {
      path: "/order",
      name: "OrderSys",
      meta: {
        title: "订单系统",
        auth: true,
        icon: "unordered-list",
      },
      component: RouteView,
      children: [
        /* 历史订单 */
        {
          path: "/order/history",
          name: "order-history",
          meta: {
            title: "历史订单",
            auth: true,
            icon: "unordered-list",

          },
          component: () =>
            import("@/views/order/history/index"),
        },
        // 订单详情
        {
          path: "/order-history/detail/:id",
          name: "order-history",
          meta: {
            title: "订单详情",
            auth: true,
            icon: "unordered-list",

          },
          component: () =>
            import("@/views/order/history/components/detail"),
        },

        {
          path: "/order-dispatch/detail/:id",
          name: "order-dispatch",
          meta: {
            title: "订单详情",
            auth: true,
            icon: "unordered-list",

          },
          component: () =>
            import("@/views/order/dispatch/components/detail"),
        },
        /* 新商家订单 */
        {
          path: "/order/dispatch",
          name: "order-dispatch",
          meta: {
            title: "新商家订单",
            auth: true,
            icon: "unordered-list",

          },
          component: () =>
            import("@/views/order/dispatch/index"),
        },
        {
          path: "/order/account",
          name: "order-account",
          meta: {
            title: "订单结算",
            auth: true,
            icon: "unordered-list",
          },
          component: () =>
            import("@/views/order/account/index"),
        },
        {
          path: "/order-account/detail/:id",
          name: "order-account",
          meta: {
            title: "结单详情",
            auth: true,
            icon: "unordered-list",
          },
          component: () =>
            import("@/views/order/account/components/detail"),
        }
      ]
    },
    {
      path: "/market",
      name: "MarketOperation",
      meta: {
        title: "市场运营",
        auth: true,
        icon: "shop",
      },
      component: RouteView,
      children: [
        /* 市场运营 */
        {
          path: "/market/market",
          name: "market",
          meta: {
            title: "集市管理",
            auth: true,
            icon: "shopping-cart",
          },
          component: () =>
            import("@/views/market/market/index"),
        },
        /* 定价策略 */
        {
          // path: "/market/price/:id",
          path: "/market/price/",
          name: "price",
          meta: {
            title: "定价策略",
            auth: true,
            icon: "shopping",
          },
          component: () =>
            import("@/views/market/price/index"),
        },
        // sku定价表
        {
          path: "/market/sku/:id",
          name: "sku",
          meta: {
            title: "定价策略",
            auth: true,
            icon: "shopping",
          },
          component: () =>
            import("@/views/market/price/components/priceTable"),
        },
        // {
        //     path: "/market/table",
        //     name: "priceTable",
        //     meta: {
        //         title: "添加商品",
        //         auth: true,
        //         icon: "shopping"
        //     },
        //     component: () =>
        //         import ("@/views/market/price/components/table"),
        // },
        {
          path: "/market/wholesaler",
          name: "wholesaler",
          meta: {
            title: "批发商管理",
            auth: true,
            icon: "inbox",
          },
          component: () =>
            import("@/views/market/wholesaler/index"),
        },
        /* cp管理 */
        {
          path: "/market/incp",
          name: "incp",
          meta: {
            title: "场内CP管理",
            auth: true,
            icon: "copyright",
          },
          component: () =>
            import("@/views/market/incp/index"),
        },
        {
          path: "/market/incpuser",
          name: "incpuser",
          meta: {
            title: "场内CP用户管理",
            auth: true,
            icon: "copyright",
          },
          component: () =>
            import("@/views/market/incpuser/index"),
        },
        {
          path: "/market/outcp",
          name: "outcp",
          meta: {
            title: "场外CP管理",
            auth: true,
            icon: "sync",
          },
          component: () =>
            import("@/views/market/outcp/index"),
        },
        {
          path: "/market/advertise",
          name: "advertise",
          meta: {
            title: "广告营销策略",
            auth: true,
            icon: "like",
          },
          component: () =>
            import("@/views/market/advertise/index"),
        },
        {
          path: "/market/notice",
          name: "notice",
          meta: {
            title: "公告管理",
            auth: true,
            icon: "notification",
          },
          component: () =>
            import("@/views/market/notice/index"),
        },
      ],
    },
    // ========================================
    {
      path: "/rider",
      name: "page3name3",
      meta: {
        title: "运力管理",
        auth: true,
        icon: "mail",
      },
      component: RouteView,
      children: [{
        path: "/rider/riderlist",
        name: "test3son1",
        meta: {
          title: "转运手管理",
          auth: true,
          icon: "mail",
        },
        component: () =>
          import("@/views/rider"),
      },
      {
        path: "/rider/riderView/:riderID",
        name: "riderView",
        hidden: true,
        meta: {
          title: "转运手-详情",
          auth: true,
          icon: "mail",
        },
        component: () =>
          import("@/views/rider/riderView"),
      },
      ],
    },
    /* 分类管理 */
    {
      path: "/system-area-type/list",
      meta: {
        title: "分类管理",
        auth: true,
      },
      name: "system-area-type",
      component: () =>
        import("@/views/systemAreaType"),
    },
    /* 集市管理 */
    {
      path: "/salemarket/list",
      meta: {
        title: "集市管理",
        auth: true,
      },
      name: "salemarket",
      component: () =>
        import("@/views/salemarket"),
    },
    /* 集市管理  区域管理*/
    {
      path: "/market/area/:id",
      name: "market",
      meta: {
        title: "区域管理",
        auth: true,
      },
      component: () =>
        import("@/views/salemarket/components/area"),
    },
    /* 批发商管理 */
    {
      path: "/merchant/list",
      meta: {
        title: "批发商管理",
        auth: true,
      },
      name: "merchant",
      component: () =>
        import("@/views/merchant"),
    },
    /* 菜单权限 */
    {
      path: "/pageRole/list",
      meta: { title: "菜单配置", auth: true },
      component: () =>
        import("@/views/pageRole"),
    },
    /* 角色菜单 */
    {
      path: "/userRoleMenu/list",
      meta: { title: "角色菜单", auth: true },
      component: () =>
        import("@/views/userRoleMenu"),
    },
    ],
  },
  ],
})

router.historyHas = () => {
  if (window.history.length > 1) {
    return true
  }
  return false
}

router.to = function (url, queryMap) {
  if (queryMap) {
    router.push({
      path: url,
      query: queryMap,
    })
    return
  }
  router.push({
    path: url,
  })
}

router.back = (url) => {
  if (window.history.length <= 1) {
    router.push({
      path: url,
    })
  } else {
    router.go(-1)
  }
}

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  if (to.matched.length === 0) {
    from.name ?
      next({
        name: from.name,
      }) :
      next("/login")
    return
  }

  // 是否页面要求必须要有token
  if (to.meta.auth) {
    // 判断该路由是否需要登录权限
    if (token.has()) {
      // 通过vuex state获取当前的token是否存在
      next()
      return
    }
    token.clear()
    next({
      path: "/login",
      query: {
        redirect: to.fullPath,
      }, // 将跳转的路由path作为参数，登录成功后跳转到该路由
    })
    return
  }

  next()
})

export default router
